#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode *next;
};


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    static unsigned int carray=0;
    if (l1==NULL && l2==NULL) {
        struct ListNode* p=NULL;
        if (carray==1) {
            p = (struct ListNode *)malloc(sizeof(struct ListNode));
            p->val = 1;
            p->next = NULL;
        } 
        return p;
    } else {
        struct ListNode* p = (struct ListNode *)malloc(sizeof(struct ListNode));
        struct ListNode* op1, *op2;
        int sum;
        
        if (l1!=NULL && l2==NULL) {
            sum = l1->val + carray;
            op1 = l1->next;
            op2 = NULL; 
        } else if(l1==NULL && l2!=NULL) {
            sum = l2->val + carray;
            op1 = NULL;
            op2 = l2->next; 
        } else { //l1!=NULL && l2!=NULL
            sum = l1->val + l2->val + carray; 
            op1 = l1->next;
            op2 = l2->next;
        }
        p->val =  sum % 10;
        carray =  sum / 10;
        p->next = addTwoNumbers(op1,op2);
        return p;
    }
}

int main() {
    struct ListNode l1;
    struct ListNode l2;

    l1.val = 0;
    l1.next = NULL;
    l2.val = 1;
    l2.next = NULL;

    struct ListNode *x = addTwoNumbers(&l1, &l2);
    printf("%d\n",x->val);
}